package com.ytktt.study.week9.task3.rpcfx.lb;

import com.ytktt.study.week9.task3.rpcfx.properties.LoadBalancerProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 负载均衡配置
 *
 * @author ytk
 */
@Configuration
@EnableConfigurationProperties(LoadBalancerProperties.class)
public class LoadBalancerConfig {

    @Bean
    @ConditionalOnProperty(prefix = LoadBalancerProperties.PREFIX, name = "type", havingValue = "random", matchIfMissing = true)
    public LoadBalancer randomLoadBalancer() {
        return new RandomLoadBalancer();
    }

    @Bean
    @ConditionalOnProperty(prefix = LoadBalancerProperties.PREFIX, name = "type", havingValue = "round_ribbon")
    public LoadBalancer roundRibbonLoadBalancer() {
        return new RoundRibbonLoadBalancer(new AtomicInteger(1));
    }
}
